package org.imaginedays.androidall.ui;

import org.imaginedays.androidall.DownloadProvider.provider.DownloadManager;

import android.content.CursorLoader;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class DownloadItemFragment extends Fragment implements LoaderCallbacks {

	private static final String KEY_CONTENT = "DownloadItemFragment:Content";

	private String mContent = "???";
	
	DownloadManager mDownloadManager;

	public static DownloadItemFragment newInstance(String content) {
		DownloadItemFragment fragment = new DownloadItemFragment();
		fragment.mContent = content;

		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if ((savedInstanceState != null)
				&& savedInstanceState.containsKey(KEY_CONTENT)) {
			mContent = savedInstanceState.getString(KEY_CONTENT);
		}
		
		mDownloadManager = new DownloadManager(getActivity().getContentResolver(), getActivity().getPackageName());
		DownloadManager.Query query = new DownloadManager.Query();
		mDownloadManager.query(query);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		TextView text = new TextView(getActivity());
		text.setGravity(Gravity.CENTER);
		text.setText(mContent);
		text.setTextSize(20 * getResources().getDisplayMetrics().density);
		text.setPadding(20, 20, 20, 20);

		LinearLayout layout = new LinearLayout(getActivity());
		layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		layout.setGravity(Gravity.CENTER);
		layout.addView(text);

		return layout;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString(KEY_CONTENT, mContent);
	}

	@Override
	public Loader onCreateLoader(int id, Bundle args) {
		
		return null;
	}

	@Override
	public void onLoadFinished(Loader id, Object arg1) {
		
	}

	@Override
	public void onLoaderReset(Loader arg0) {
		
	}

}
